package com.fulu.springcloud.ribbonserver.service;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

@Service
public class HelloService {
    @Autowired
    RestTemplate restTemplate;

    /**
     * 添加熔断处理方法，当SERVER-HI不可用时，直接执行error方法
     * @param name
     * @return
     */
    @HystrixCommand(fallbackMethod = "error")
    public String sayHi(String name){
        return restTemplate.getForObject("http://SERVER-HI/hi?name=" + name, String.class);
    }

    /**
     * 熔断执行这个方法，也可获取到参数
     * @param name
     * @return
     */
    public String error(String name){
        return "hi " + name + "，SERVER-HI出错了";
    }
}
